package com.ly.erms.generator.action.config;

import java.io.File;
import java.util.List;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.ly.erms.core.util.FileUtil;
import com.ly.erms.generator.engine.SimpleTemplateEngine;
import com.ly.erms.generator.engine.base.ErmsTemplateEngine;
import com.ly.erms.generator.engine.config.ContextConfig;
import com.ly.erms.generator.engine.config.SqlConfig;

/**
 * 代码生成抽象配置
 * 
 * @author liuyang
 * @date 2018年1月11日
 */
public abstract class AbstractGeneratorConfig {
	/**
	 * mybatis-plus代码生成器配置
	 */
	GlobalConfig globalconfig = new GlobalConfig();

	DataSourceConfig dataSourceConfig = new DataSourceConfig();

	StrategyConfig strategyConfig = new StrategyConfig();

	PackageConfig packageConfig = new PackageConfig();

	TableInfo tableInfo = null;

	/**
	 * erms 代码生成器配置
	 */
	ContextConfig contextConfig = new ContextConfig();

	SqlConfig sqlConfig = new SqlConfig();

	protected abstract void config();

	public void init() {
		config();
		packageConfig
				.setService(contextConfig.getProPackage() + ".modular." + contextConfig.getModuleName() + ".service");
		packageConfig.setServiceImpl(
				contextConfig.getProPackage() + ".modular." + contextConfig.getModuleName() + ".service.impl");

		// controller没用掉,生成之后会自动删掉
		packageConfig.setController("TTT");

		if (!contextConfig.getEntitySwitch()) {
			packageConfig.setEntity("TTT");
		}

		if (!contextConfig.getDaoSwitch()) {
			packageConfig.setMapper("TTT");
			packageConfig.setXml("TTT");
		}

		if (!contextConfig.getServiceSwitch()) {
			packageConfig.setService("TTT");
			packageConfig.setServiceImpl("TTT");
		}
	}

	public void doMpGeneration() {
		init();
		AutoGenerator autoGenerator = new AutoGenerator();
		autoGenerator.setGlobalConfig(globalconfig);
		autoGenerator.setDataSource(dataSourceConfig);
		autoGenerator.setStrategy(strategyConfig);
		autoGenerator.setPackageInfo(packageConfig);
		autoGenerator.execute();
		destory();

		// 获取table信息,用于erms代码生成
		List<TableInfo> tableInfoList = autoGenerator.getConfig().getTableInfoList();
		if (tableInfoList != null && tableInfoList.size() > 0) {
			this.tableInfo = tableInfoList.get(0);
		}
	}

	/**
	 * 删除不必要的代码
	 */
	public void destory() {
		String outputDir = globalconfig.getOutputDir() + "/TTT";
		FileUtil.deleteDir(new File(outputDir));
	}

	public AbstractGeneratorConfig() {
	}
	
	public void doErmsGeneration(){
		ErmsTemplateEngine ermsTemplateEngine = new SimpleTemplateEngine();
		ermsTemplateEngine.setContextConfig(contextConfig);
		sqlConfig.setConnection(dataSourceConfig.getConn());
		ermsTemplateEngine.setSqlConfig(sqlConfig);
		ermsTemplateEngine.setTableInfo(tableInfo);
		ermsTemplateEngine.start();
	}
}
